//
//  SYHouSanMingViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/6/19.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYHouSanMingViewController.h"
#import "BanJiTopView.h"
#import "SYHoSanMingView.h"
#import "SYHoSanMingTableViewCell.h"
#import "ZXFDCreateClassView.h"


@interface SYHouSanMingViewController () <BanJiTopViewSelectedDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) BanJiTopView *banJiTopView;
@property (strong, nonatomic) UIButton *chakanBtn;//退出
@property (strong, nonatomic) UILabel *className;//班级名称
@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (strong, nonatomic) SYNoLabel *nolab;
@property (nonatomic, strong) ZXFDCreateClassView *createClassView;
@property (copy, nonatomic) NSString *cjClassName;//辅导班级名字
@property (copy, nonatomic) NSString *fdbjId;//辅导班级id
/**
 创建班级
 */
@property (nonatomic, strong) UIButton *createClassButton;
@end

@implementation SYHouSanMingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"#e8e8e8"];
//    [self requestData];
    self.nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
//    [self.nolab mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.banJiTopView.mas_bottom);
//        make.left.right.bottom.equalTo(self.view);
//    }];
    self.nolab.backgroundColor = [UIColor clearColor];
    [_curTableView headerRefreshBlock:^{
        [self requestData];
    }];
    [_curTableView.mj_header beginRefreshing];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismissChuangJianBanJi) name:@"ZaiXianFuDaoTianJiaXueShengView" object:nil];
}
//删除掉
- (void)dismissChuangJianBanJi{
    [UIView animateWithDuration:0.5 animations:^{
        self->_createClassView.frame = CGRectMake(0, self.view.frame.size.width,self.view.frame.size.width,self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [self->_createClassView removeFromSuperview];
    }];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ZaiXianFuDaoTianJiaXueShengView" object:nil];
}
#pragma mark - 创建班级
- (UIButton *)createClassButton{
    if (!_createClassButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"创建班级" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.layer.cornerRadius = 2;
        button.layer.borderWidth = 1;
        button.layer.borderColor = SELECTEDCOLOR.CGColor;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(createClassClick:) forControlEvents:UIControlEventTouchUpInside];
        _createClassButton = button;
    }
    return _createClassButton;
}
#pragma mark - 前往创建班级
- (void)createClassClick:(UIButton *)sender{
    NSLog(@"前往创建班级");
    _createClassView = [[ZXFDCreateClassView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _createClassView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    _createClassView.mesgId = self.xgModel.msgId;
    _createClassView.isMessage = YES;
    _createClassView.fdbjName = self.cjClassName;
    _createClassView.fdbjId = self.fdbjId;
    _createClassView.className = @"成绩后三名";
    _createClassView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_createClassView];
    [_createClassView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@20);
        make.bottom.equalTo(@0);
    }];
}
- (void)requestData {
    [self.dataListArr removeAllObjects];
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    
    NSString *mesid = self.xgModel.msgId;
    if (mesid.length == 0) {
        mesid = @"4";
    }
    
    [SYRequestAPI sy_getExamLastThreeStudentInfoMsgId:mesid success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        NSLog(@"成绩后三名 %@",dataListArr);
        if (dataListArr.count > 0) {
            self.nolab.hidden = YES;
            SYHoSanMingModel *kqmodel = dataListArr.firstObject;
            weakSelf.className.text = [NSString stringWithFormat:@"班级名称:%@     考试名称:%@",kqmodel.className, kqmodel.templateTitle];   SYStringWithPickFormat(@"班级名称： ", kqmodel.className);
            
            weakSelf.cjClassName = kqmodel.className;
            weakSelf.fdbjId = kqmodel.classId;
            
        } else {
            self.nolab.hidden = NO;
        }
        
        weakSelf.dataListArr = dataListArr;
        [weakSelf.curTableView reloadData];
        [self.curTableView.mj_header endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        self.nolab.hidden = NO;
        [self.curTableView.mj_header endRefreshing];
    }];
    
    
    
//    [SYRequestAPI sy_getAttendanceStudentInfoMsgid:mesid isAttend:self.isAttend success:^(NSMutableArray *dataListArr) {
//        [MBProgressHUD mj_hideHUDForView:self.view];
//        if (dataListArr.count > 0) {
//            self.nolab.hidden = YES;
//            SYKaoQinYiChangModel *kqmodel = dataListArr.firstObject;
//            weakSelf.className.text = SYStringWithPickFormat(@"班级名称： ", kqmodel.ClassName);
//        } else {
//            self.nolab.hidden = NO;
//        }
//        weakSelf.listArr = dataListArr;
//        [weakSelf.curTableView reloadData];
//    } fail:^(NSError *error) {
//        [MBProgressHUD mj_hideHUDForView:self.view];
//
//    }];
}


- (void)initSubView {
    [self.view addSubview:self.banJiTopView];
    [self.banJiTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
    //退出
    [self.banJiTopView addSubview:self.chakanBtn];
    [self.chakanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.banJiTopView.mas_centerY);
        make.right.equalTo(self.banJiTopView).offset(-25);
        make.width.height.mas_equalTo(40);
    }];
    //班级名称
    [self.banJiTopView addSubview:self.className];
    [self.className mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.banJiTopView.mas_centerY);
        make.left.equalTo(self.banJiTopView).offset(25);
        
    }];
    
    [self.view addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.banJiTopView.mas_bottom);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.bottom.equalTo(self.view).offset(-20);
    }];
    
}
#pragma mark - tabl

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SYHoSanMingTableViewCell *hsmCell = [tableView dequeueReusableCellWithIdentifier:@"SYHoSanMingTableViewCell" forIndexPath:indexPath];
    if (indexPath.row %2) {
        hsmCell.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    } else {
        hsmCell.backgroundColor = [UIColor whiteColor];
    }
    SYHoSanMingModel *hsmModel = self.dataListArr[indexPath.row];
    hsmCell.hsmModel = hsmModel;
    return hsmCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    
    SYHoSanMingView *topView = [[NSBundle mainBundle] loadNibNamed:@"SYHoSanMingView" owner:nil options:nil].firstObject;


    [headerView addSubview:topView];

    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(headerView);
        make.left.equalTo(headerView.mas_left).offset(0);
        make.right.equalTo(headerView.mas_right).offset(0);
    }];

    
    
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width , 50)];
    view.backgroundColor = [UIColor whiteColor];
        [view addSubview:self.createClassButton];
        [_createClassButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view.mas_centerX);
            make.width.equalTo(@80);
            make.height.equalTo(@40);
            make.top.equalTo(view.mas_top).offset(10);
        }];
    return view;
}
#pragma mark - BanJiTopViewSelectedDelegate
//无点明及课训记录
- (void)leftButtonClick {
//    self.isAttend = @"1";
//    [self requestDataListArr];
}
//有点名记录无课训成绩
- (void)rightButtonClick {
//    self.isAttend = @"2";
//    [self requestDataListArr];
}
//退出
- (void)backBtnClick {
    //    [UIView animateWithDuration:kAnimate animations:^{
    //        self.frame = CGRectMake(0, kScreen_HEIGHT, APP_WIDTH, APP_HEIGHT-20);
    //    } completion:^(BOOL finished) {
    //        [self removeFromSuperview];
    //    }];
    [self.view removeFromSuperview];
//    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark- set
- (BanJiTopView *)banJiTopView {
    if (!_banJiTopView) {
        _banJiTopView = [[BanJiTopView alloc] init];
        _banJiTopView.delegate = self;
        _banJiTopView.timeButton.hidden = YES;
        [_banJiTopView.weiJieYeButton setTitle:@"" forState:UIControlStateNormal];
        [_banJiTopView.jieYeButton setTitle:@"" forState:UIControlStateNormal];
        _banJiTopView.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        _banJiTopView.lineView.hidden = YES;
    }
    return _banJiTopView;
}

- (UIButton *)chakanBtn {
    if (!_chakanBtn) {
        _chakanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //        [_chakanBtn setTitle:@"查看试卷" forState:UIControlStateNormal];
        //        [_chakanBtn setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [_chakanBtn setImage:[UIImage imageNamed:@"fudao_close"] forState:UIControlStateNormal];
        _chakanBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_chakanBtn setTitleColor:SELECTEDCOLOR forState:0];
        [_chakanBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chakanBtn;
}

- (UILabel *)className {
    if (!_className) {
        _className = [UILabel titleColour:TITLECOLOR titleFount:14 pingfang:@"PingFang-SC-Regular" alignment:NSTextAlignmentLeft titleLine:1];
        _className.text = @"班级名称:   考试名称:考试卷名称名称名称";
    }
    return _className;
}
//表
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYHoSanMingTableViewCell"]];
    }
    return _curTableView;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
